/*
 * @author java_deng
 * @date 2025/2/5 11:46
 * @description
 */
package org.hbwl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.hbwl.dto.Animal;
import org.hbwl.dto.Region;
import org.hbwl.mapper.AnimalMapper;
import org.hbwl.mapper.RegionMapper;
import org.hbwl.po.AnimalPo;
import org.hbwl.utils.PageResponseData;
import org.hbwl.vo.AnimalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnimalService {
    @Autowired
    private AnimalMapper animalMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private RegionService regionService;

    public PageResponseData<AnimalVo> list(AnimalPo animalPo) {
        if(animalPo.getRegionId()!=null) {
            Long addressId = animalPo.getRegionId().get(2);
            if (addressId == 0) {
                addressId = animalPo.getRegionId().get(1);
            }
            animalPo.setAddressId(addressId);
        }
        animalPo.setStatus(3);
        Page<AnimalVo> result = PageHelper.startPage(animalPo.getPageIndex(), animalPo.getPageSize());
        animalMapper.list(animalPo);
        PageResponseData<AnimalVo> pageResponseData= new PageResponseData<>(result.getResult(),result.getTotal(),result.getPages());
        return pageResponseData;
    }

    public void update(AnimalVo animalVo) {
        if(animalVo.getRegionId()!=null){
            Long addressId = animalVo.getRegionId().get(2);
            if(addressId==0){
                addressId=animalVo.getRegionId().get(1);
            }
            animalVo.setAddressId(addressId);
            Region region = regionMapper.selectById(addressId);
            animalVo.setAddress(region.getRegionName());
            String fullAddress=regionService.getFullAddress(region);
            animalVo.setFullAddress(fullAddress);
        }
        animalVo.setStatus(1);
        animalMapper.updateById(animalVo);
    }

    public void add(AnimalVo animalVo) {
        Long addressId = animalVo.getRegionId().get(2);
        if(addressId==0){
            addressId=animalVo.getRegionId().get(1);
        }
        animalVo.setAddressId(addressId);
        Region region = regionMapper.selectById(addressId);
        animalVo.setAddress(region.getRegionName());
        String fullAddress=regionService.getFullAddress(region);
        animalVo.setFullAddress(fullAddress);
        animalVo.setStatus(1);
        animalMapper.insert(animalVo);
    }

    public AnimalVo get(Long id) {
        return animalMapper.detail(id);
    }

    public void delete(Long id) {
        animalMapper.deleteById(id);
    }

    public PageResponseData managerList(AnimalPo animalPo) {
        Page<AnimalVo> result = PageHelper.startPage(animalPo.getPageIndex(), animalPo.getPageSize());
        animalMapper.list(animalPo);
        PageResponseData<AnimalVo> pageResponseData= new PageResponseData<>(result.getResult(),result.getTotal(),result.getPages());
        return pageResponseData;
    }

    public void audit(AnimalVo animalVo) {
        animalMapper.updateById(animalVo);
    }
}
